package com.hk.employeemanagementsystem.service.impl;

import com.hk.employeemanagementsystem.mapper.MenuEntityMapper;
import com.hk.employeemanagementsystem.entity.MenuEntity;
import com.hk.employeemanagementsystem.mapper.MenuRoleMapper;
import com.hk.employeemanagementsystem.service.MenuEntityService;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author by 何坤
 * @Classname MenuEntityServiceImpl
 * @Description TODO
 * @Date 2019/12/13 15:51
 */
@Service
@Transactional
public class MenuEntityServiceImpl implements MenuEntityService {

    @Resource
    private MenuEntityMapper menuEntityMapper;

    @Resource
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<MenuEntity> findMenuEntityByRoleId(Integer id) {
        return menuEntityMapper.selectMenuEntityByRoleId(id);
    }

    @Override
    public List<Integer> getMenusByRId(Integer rid) {
        return menuEntityMapper.getMenusByRId(rid);
    }

//    @Cacheable(value = "menu", key = "tree", unless = "#result==null")
    @Override
    public List<MenuEntity> selectAllMenu() {
        return menuEntityMapper.selectAllMenu();
    }

//    @Cacheable(value = "menu", key = "tree", unless = "#result==null")
    @Override
    public List<MenuEntity> menuTree() {
        return menuEntityMapper.menuTree();
    }

    @Override
    public int updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteMenuById(rid);
        if (mids.length == 0) {
            return 0;
        }
        return menuRoleMapper.addMenuRole(rid, mids);
    }
}
